import org.json.JSONObject;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Frame extends JFrame {
    //测试更新
    //连接服务器
    private class ConnectServer implements Runnable {
        @Override
        public void run() {
            try {
                socket = new Socket("169.254.103.146",5050);
                sending = false;
                txtInfo.setText(txtInfo.getText() + "服务器连接成功\n");
                return;
            } catch (IOException e) {
                run();
            } finally {
                txtInfo.setText(txtInfo.getText() + "与服务器取得联系，提交本机场信息…………\n");
                sendInfo();
                try {
                    response();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }




    private Vector tableTitle = new Vector();
    private Vector<Vector> tableData = new Vector<>();

    /**
     * 操作表格
     * */
    private class TableModel extends AbstractTableModel {

        @Override
        public int getRowCount() {
            return tableData.size();
        }

        @Override
        public int getColumnCount() {
            return tableTitle.size();
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return tableData.get(rowIndex).get(columnIndex);
        }

        @Override
        public String getColumnName(int column) {
            return (String) tableTitle.get(column);
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }
    }

    FileInfo file = new FileInfo();
    Socket socket;

    String filePath;//文件路径
    boolean sending = false;//是否正在发送



    //表格
    JTable tableAirport;
    //多行文本域
    JTextArea txtInfo = new JTextArea(15,30);
    JScrollPane txtPane = new JScrollPane(txtInfo,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    JLabel label = new JLabel("监控台");


    //创建线程池
    ExecutorService es = Executors.newFixedThreadPool(10);


    public Frame(String title) throws IOException {
        super(title);
        Container container = getContentPane();
        container.setLayout(new BorderLayout());

        //头部
        container.add(label,BorderLayout.NORTH);
        container.add(txtPane,BorderLayout.CENTER);

        tableTitle.add("文件名");
        tableTitle.add("版本号");
        tableTitle.add("更新日期");

        //底部
        DefaultTableModel model = new DefaultTableModel(tableData,tableTitle);
        tableAirport = new JTable(model);
        container.add(new JScrollPane(tableAirport),BorderLayout.SOUTH);

        //开始连接服务器
        txtInfo.setText(txtInfo.getText() + "正在连接服务器…………\n");
        es.execute(new ConnectServer());
    }

    private void sendInfo() {
        try {
            OutputStream outputStream = socket.getOutputStream();
            Airport airport = new Airport("机场1",InetAddress.getLocalHost().getHostAddress() + "");
            JSONObject jsonObject = new JSONObject(airport);
            jsonObject.put("cmd","addAirport");
            outputStream.write(jsonObject.toString(2).getBytes());
            return;
        } catch (IOException e) {
            txtInfo.setText(txtInfo.getText() + "提交本机场信息失败，正在重试\n");
            sendInfo();
        }
    }

    //应答机制
    private void response() throws IOException {
        while (true) {
            try {
                InputStream inputStream = socket.getInputStream();
                byte[] inputData = new byte[1024];
                String msg = new String(inputData,0,inputStream.read(inputData),"UTF-8");
                JSONObject jsonObject = new JSONObject(msg);
                String cmd = jsonObject.getString("cmd").trim();
                switch (cmd) {
                    case "addSucceed" : {
                        txtInfo.setText(txtInfo.getText() + "成功与服务器取得联系，已提交本机场信息\n");
                        break;
                    }
                    case "update" : {
                        System.out.println("收到更新提醒");
                    }
                }
            } catch (IOException e) {
                System.out.println(e);
                continue;
            }
        }

    }
}
